import json

from bs4 import BeautifulSoup
from loguru import logger

from com.arcfox.manager.redis_task_manager import RedisTaskManager
from com.arcfox.school.processor.base_processor import BaseProcessor
from com.arcfox.middleware import async_mysql_middleware as db
from com.arcfox.util.muilty_coroutine_util import concurrency


class LineScoreProcessor(BaseProcessor):

    def __init__(self):
        super().__init__()

    async def store_province(self, province_data_list):
        mapping = db.MysqlMapping("tb_province_score")
        for province_data in province_data_list:
            exist = await mapping.query(
                {
                    "province_name": province_data["province_name"],
                    "subject_item": province_data["subject_item"],
                    "score_year": province_data["score_year"],
                    "batch_name": province_data["batch_name"]
                }, ["id"])
            if exist:
                logger.info("已经存在{}", province_data)
                continue
            await mapping.insert(province_data)

    async def store_school_score_province(self, data_list):
        mapping = db.MysqlMapping("tb_university_score")
        for data in data_list:
            university_uid = self.generate_school_uid(data["university_name"])
            data["university_uid"]=university_uid
            exist = await mapping.query(
                {
                    "university_uid": university_uid,
                    "province": data["province"],
                    "subject_item": data["subject_item"],
                    "score_year": data["score_year"],
                    "batch_name": data["batch_name"]
                }, ["id"])
            if exist:
                logger.info("已经存在data:{},exist:{}", data,exist)
                # continue
            await mapping.insert(data)
